package gestion.camping.vue;

import gestion.camping.controleur.FraisReservationController;
import gestion.camping.dto.FraisReservation;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ViewFraisReservationDetail extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -17760458983364266L;
	
	
	private JButton btnEnregister;
	private JButton btnAnnuler;
	
	private FraisReservation fraisReservation;
	private FraisReservationController controller;
	private JButton btnRetourListe;
	private JLabel lblAnnee;
	private JLabel lblPrix;
	private JTextField textFieldAnnee;
	private JTextField textFieldPrix;


	/**
	 * Create the frame.
	 */
	public ViewFraisReservationDetail(FraisReservationController controller) {
	
		this.controller = controller; 
		this.fraisReservation = controller.getFraisReservation();
		
		initCompoment();
		
		setModal(true);
	}
	
	public void initCompoment(){
	
		
		setBounds(0, 25, 700, 500);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0,0, 0, 0};
		gridBagLayout.rowHeights = new int[]{40, 40, 25, 25, 25, 25, 25, 25, 0};
		gridBagLayout.columnWeights = new double[]{0.3, 0.2, 0.2, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		getContentPane().setLayout(gridBagLayout);
		
		btnRetourListe = new JButton(MessagesUtil.formatMessage("fraisReservation.ajout.btn.retour"));
		btnRetourListe.addActionListener(this);
		GridBagConstraints gbc_btnRetourListe = new GridBagConstraints();
		gbc_btnRetourListe.anchor = GridBagConstraints.WEST;
		gbc_btnRetourListe.insets = new Insets(0, 0, 5, 0);
		gbc_btnRetourListe.gridx = 3;
		gbc_btnRetourListe.gridy = 0;
		getContentPane().add(btnRetourListe, gbc_btnRetourListe);
		
		
		btnEnregister = new JButton(MessagesUtil.formatMessage("fraisReservation.ajout.btn.enregistrer"));
		btnEnregister.addActionListener(this);
		
		lblAnnee = new JLabel(MessagesUtil.formatMessage("fraisReservation.ajout.annee"));
		GridBagConstraints gbc_lblAnnee = new GridBagConstraints();
		gbc_lblAnnee.anchor = GridBagConstraints.EAST;
		gbc_lblAnnee.insets = new Insets(0, 0, 5, 5);
		gbc_lblAnnee.gridx = 1;
		gbc_lblAnnee.gridy = 3;
		getContentPane().add(lblAnnee, gbc_lblAnnee);
		
		textFieldAnnee = new JTextField(String.valueOf(fraisReservation.getAnnee()));
		GridBagConstraints gbc_textFieldAnnee = new GridBagConstraints();
		gbc_textFieldAnnee.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldAnnee.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldAnnee.gridx = 2;
		gbc_textFieldAnnee.gridy = 3;
		getContentPane().add(textFieldAnnee, gbc_textFieldAnnee);
		textFieldAnnee.setColumns(10);
		
		lblPrix = new JLabel(MessagesUtil.formatMessage("fraisReservation.ajout.prix"));
		GridBagConstraints gbc_lblPrix = new GridBagConstraints();
		gbc_lblPrix.anchor = GridBagConstraints.EAST;
		gbc_lblPrix.insets = new Insets(0, 0, 5, 5);
		gbc_lblPrix.gridx = 1;
		gbc_lblPrix.gridy = 4;
		getContentPane().add(lblPrix, gbc_lblPrix);
		
		textFieldPrix = new JTextField(String.valueOf(fraisReservation.getPrix()));
		GridBagConstraints gbc_textFieldPrix = new GridBagConstraints();
		gbc_textFieldPrix.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldPrix.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldPrix.gridx = 2;
		gbc_textFieldPrix.gridy = 4;
		getContentPane().add(textFieldPrix, gbc_textFieldPrix);
		textFieldPrix.setColumns(10);
		GridBagConstraints gbc_btnEnregister = new GridBagConstraints();
		gbc_btnEnregister.anchor = GridBagConstraints.EAST;
		gbc_btnEnregister.insets = new Insets(0, 0, 5, 5);
		gbc_btnEnregister.gridx = 2;
		gbc_btnEnregister.gridy = 7;
		getContentPane().add(btnEnregister, gbc_btnEnregister);
		
		btnAnnuler = new JButton(MessagesUtil.formatMessage("fraisReservation.ajout.btn.annuler"));
		btnAnnuler.addActionListener(this);
		GridBagConstraints gbc_btnAnnuler = new GridBagConstraints();
		gbc_btnAnnuler.anchor = GridBagConstraints.WEST;
		gbc_btnAnnuler.insets = new Insets(0, 0, 5, 0);
		gbc_btnAnnuler.gridx = 3;
		gbc_btnAnnuler.gridy = 7;
		getContentPane().add(btnAnnuler, gbc_btnAnnuler);
	}
	
	public void update(){
		/*mise � jour des label... */
		textFieldAnnee.setText(String.valueOf(fraisReservation.getAnnee()));
		textFieldPrix.setText(String.valueOf(fraisReservation.getPrix()));
		
		this.repaint();
	}

	public FraisReservation getFraisReservation() {
		return fraisReservation;
	}

	public void setFraisReservation(FraisReservation fraisReservation) {
		this.fraisReservation = fraisReservation;		
		update();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == this.btnEnregister){
			/*alimentation de l'objet saison*/
			fraisReservation.setAnnee(Integer.valueOf(textFieldAnnee.getText()));
			fraisReservation.setPrix(Integer.valueOf(textFieldPrix.getText()));
			
			controller.enregistrer(fraisReservation);
			
		}else if(e.getSource() == this.btnAnnuler){
			update();
		}else if(e.getSource() == this.btnRetourListe){
			
			this.dispose();
			controller.retour();
			
		}
	}
		
}
